var worker = worker || {};//改变颜色等一系列函数的对象。

/*规定了逻辑长度的数据格式。
worker.save = function(mass){
	var stringArray = new Array;
	var tempCube;
	
	stringArray[0] =	0+
						mass.length.toString(10).longFixed(6)+
						(mass.localX+648).toString(36).longFixed(2)+
						(mass.localY+648).toString(36).longFixed(2)+
						(mass.localZ+648).toString(36).longFixed(2);
						
	for(var nCube=0;nCube < mass.length;nCube++){
		if(!mass[nCube][0]){
			tempCube = mass[nCube];
			stringArray[stringArray.length] = 	(tempCube.alpha*10).toString(16)+
												parseInt(tempCube.red*255).toString(16).longFixed(2)+
												parseInt(tempCube.green*255).toString(16).longFixed(2)+
												parseInt(tempCube.blue*255).toString(16).longFixed(2)+
												(tempCube.localX+648).toString(36).longFixed(2)+
												(tempCube.localY+648).toString(36).longFixed(2)+
												(tempCube.localZ+648).toString(36).longFixed(2);
			}
			else{
				stringArray[stringArray.length] = worker.save(mass[nCube]);
				}
		}
		
	return stringArray.join("");
	}

worker.create = function(stringData){
	worker._justCreate(stringData);
	var nowArea = cubeSelect[cubeSelect.length-1];
	worker.reComputeBounding(nowArea[nowArea.length-1]);
	}



worker._justCreate = function(stringData){
	
	var newMass = new Array;
		newMass.transform = cubePack.createObject("Transform");
		newMass.transform.parent = cubeSelect[cubeSelect.length-1].transform;
		newMass.parent = cubeSelect[cubeSelect.length-1];
		newMass.localX = parseInt(stringData.substr(7,2),36)-648;
		newMass.localY = parseInt(stringData.substr(9,2),36)-648;
		newMass.localZ = parseInt(stringData.substr(11,2),36)-648;
		newMass.x = newMass.localX + newMass.parent.x;
		newMass.y = newMass.localY + newMass.parent.y;
		newMass.z = newMass.localZ + newMass.parent.z;
		newMass.transform.translate(newMass.localX,newMass.localY,newMass.localZ);
	cubeSelect[cubeSelect.length-1].push(newMass);
	cubeSelect.push(newMass);
	var shiftN = 0;
	var massLength = parseInt(stringData.substr(1,6),10);
	for(var n=0;n < massLength;n++){
		if(parseInt(stringData.substr((n+shiftN+1)*13,1),16)){
			worker.createACubeByString(stringData.substr((n+shiftN+1)*13,13));
			}
			else{
				shiftN += worker._justCreate(stringData.substr((n+shiftN+1)*13));
				}
		}
		
	cubeSelect.pop();
	return massLength;
	}

worker.createACubeByString = function(stringData){
	var newCube = new creator.copyCube(	shape[0].cubeShape,
										parseInt(stringData.substr(1,2),16)/255,
										parseInt(stringData.substr(3,2),16)/255,
										parseInt(stringData.substr(5,2),16)/255,
										parseInt(stringData.substr(0,1),16)/10,
										parseInt(stringData.substr(7,2),36)-648,
										parseInt(stringData.substr(9,2),36)-648,
										parseInt(stringData.substr(11,2),36)-648,
										1);
			if(newCube.transform){
				cubeSelect[cubeSelect.length-1].push(newCube);
				}
	}
*/
worker.save = function(mass){
	var stringArray = new Array;
	var tempCube;
	
	stringArray[0] =	"["+
						(mass.localX+648).toString(36).longFixed(2)+
						(mass.localY+648).toString(36).longFixed(2)+
						(mass.localZ+648).toString(36).longFixed(2)+
						"-";
	for(var nCube=0;nCube < mass.length;nCube++){
		if(!mass[nCube][0]){
			tempCube = mass[nCube];
			stringArray[stringArray.length] = 	(tempCube.alpha*10).toString(16)+
												parseInt(tempCube.red*255).toString(16).longFixed(2)+
												parseInt(tempCube.green*255).toString(16).longFixed(2)+
												parseInt(tempCube.blue*255).toString(16).longFixed(2)+
												(tempCube.localX+648).toString(36).longFixed(2)+
												(tempCube.localY+648).toString(36).longFixed(2)+
												(tempCube.localZ+648).toString(36).longFixed(2);
			}
			else{
				stringArray[stringArray.length] = worker.save(mass[nCube]);
				}
		}
	stringArray[stringArray.length] = "]";
	return stringArray.join("");
	}

worker.create = function(stringData){
	worker._justCreate(stringData);
	}

worker._justCreate = function(stringData){
	var shiftN = 0;
	var n = 0;
	var createN = 0;
	while(n*13+shiftN < stringData.length){
		var label = stringData.substr(n*13+shiftN,1);
		if(label == "["){
			var newMass = new Array;
				newMass.transform = cubePack.createObject("Transform");
				newMass.transform.parent = cubeSelect[cubeSelect.length-1].transform;
				newMass.parent = cubeSelect[cubeSelect.length-1];
				newMass.localX = parseInt(stringData.substr(n*13+shiftN+1,2),36)-648;
				newMass.localY = parseInt(stringData.substr(n*13+shiftN+3,2),36)-648;
				newMass.localZ = parseInt(stringData.substr(n*13+shiftN+5,2),36)-648;
				newMass.x = newMass.localX + newMass.parent.x;
				newMass.y = newMass.localY + newMass.parent.y;
				newMass.z = newMass.localZ + newMass.parent.z;
				newMass.transform.translate(newMass.localX,newMass.localY,newMass.localZ);
			cubeSelect[cubeSelect.length-1].push(newMass);
			cubeSelect.push(newMass);
			shiftN += 8;
			worker._justCreate(stringData.substr(n*13+shiftN));
			break;
			}
		else if(label == "]"){
			cubeSelect.pop();
			shiftN += 1;
			if(!createN && n){
				//删除这个空的组
				var tempMass = cubeSelect[cubeSelect.length-1][cubeSelect[cubeSelect.length-1].length-1];
				cubePack.removeObject(tempMass.transform);
				tempMass.transform.parent = null;
				tempMass.parent = null;
				cubeSelect[cubeSelect.length-1].pop();
				}
			}
		else{
			var newCube = worker.createACubeByString(stringData.substr(n*13+shiftN,13));
			if(newCube){createN++;}
			n++;
			}
		}
	return newMass;
	}

worker.createACubeByString = function(stringData){
	var newCube = new creator.copyCube(	shape[0].cubeShape,
										parseInt(stringData.substr(1,2),16)/255,
										parseInt(stringData.substr(3,2),16)/255,
										parseInt(stringData.substr(5,2),16)/255,
										parseInt(stringData.substr(0,1),16)/10,
										parseInt(stringData.substr(7,2),36)-648,
										parseInt(stringData.substr(9,2),36)-648,
										parseInt(stringData.substr(11,2),36)-648,
										1);
			if(newCube.transform){
				newCube.comAndUpDataBounding();
				cubeSelect[cubeSelect.length-1].push(newCube);
				return newCube;
				}
	}












































worker.capture = function(){
	var o3dDiv = document.getElementById("o3d");
	o3dDiv.style.width = "256px";
	o3dDiv.style.height = "256px";
	}

worker.updataAllLight = function(){
	shape[0].updataLight();
	moveArrow.updataLight();
	growthArrow.updataLight();
	}

worker.changeOtherAlpha = function(mass,alpha,just){
	if(cubeSelectNow[cubeSelectNow.length-1]){
		worker._alphaIn(mass,alpha);
		}
		else{
			worker._alphaOut(mass,just);
			}
	}
worker._alphaIn = function(mass,alpha){
	worker.changeAlpha(mass.parent,alpha);
	worker.changeAlpha(mass,1);
	}
	
worker._alphaOut = function(mass,just){
	worker.changeAlpha(mass,1);
	}

worker.changeAlpha = function(mass,alphaFactor){
	for(var n=0;n < mass.length;n++){
		if (mass[n][0]){
			worker.changeAlpha(mass[n],alphaFactor);
			}
			else{ mass[n].transform.getParam("diffuse").value = [
																 mass[n].red,
																 mass[n].green,
																 mass[n].blue,
																 alphaFactor*mass[n].alpha
																 ];
			}
		}
	}



//删除cube/cubeMass的函数
worker.removeCubesXYZ = function(cubes){
	var fathers = new Array;
	var testCube;
	for (var nCubes=0;nCubes < cubes.length;nCubes++){
		var thisCube = cubes[nCubes];
		thisCube.clearRecord = true;
		var newFather = true;
		for (var nFather=0;nFather < fathers.length;nFather++){
			if(fathers[nFather]==thisCube.parent){
				newFather = false;
				}
			}
		if(newFather){
			fathers.push(thisCube.parent);
			}
		}
	var cubeSelectNowLength = cubeSelectNow.length;
	for(var nSelect=cubeSelectNowLength-1;nSelect>=0;nSelect--){
		if(!cubeSelectNow[nSelect][2][0]){
				testCube = cubeSelectNow[nSelect][2];
				if(testCube.clearRecord){
					creator.clearWireFrame(nSelect);
					cubeSelectNow.splice(nSelect,1);
					}
			}
		}
		
		
	for(var nFather=0;nFather < fathers.length;nFather++){
		var thisMass = fathers[nFather];
		var thisMassLength = thisMass.length
		for(var nCubes=thisMassLength-1;nCubes>=0;nCubes--){
			if(!thisMass[nCubes][0]){
				testCube = thisMass[nCubes];
				if(testCube.clearRecord){
					testCube.transform.parent = null;
					thisMass.splice(nCubes,1);
					cubeCoordArray[testCube.x][testCube.y][testCube.z] = null;
					}
				}
			}
		worker.clearNochildParentByNotUpBounding(thisMass);
		}
	}

worker.clearNochildParentByNotUpBounding = function(thisMass){
	//检测是否已经没有子集，如果没有则删除这个mass
		if(thisMass != cubeMass){
			if(!thisMass[0]){
			thisMass.massClearRecord = true;
			var massFather = thisMass.parent;
				for(var nMass=0;nMass < massFather.length;nMass++){
					if (massFather[nMass].massClearRecord){
						massFather.splice(nMass,1);
						cubeSelect.pop();
						view.resetView();
						worker.changeAlpha(cubeSelect[cubeSelect.length-1],1);
						worker.clearNochildParentByNotUpBounding(massFather);
						break;
						}
					}
				}
			}	
	}

worker.clearNochildParent = function(thisMass){
	//检测是否已经没有子集，如果没有则删除这个mass
		if(thisMass != cubeMass){
			if(!thisMass[0]){
			thisMass.massClearRecord = true;
			var massFather = thisMass.parent;
				for(var nMass=0;nMass < massFather.length;nMass++){
					if (massFather[nMass].massClearRecord){
						massFather.splice(nMass,1);
						cubeSelect.pop();
						view.resetView();
						worker.changeAlpha(cubeSelect[cubeSelect.length-1],1);
						worker.clearNochildParent(massFather);
						break;
						}
					}
				}
				else{
					//如果还是有子集的，就更新一次bounding
					worker.reAddBoundingByChild(thisMass);
				}
			}	
	}



worker.removeCubes = function(){
	if(cubeSelectNow[0]){
		creator.removeAllFrames();
		var cubeSelectNowLength = cubeSelectNow.length;
		 	for (var n=cubeSelectNowLength-1;n>=0;n--)
			  {
				 worker.removeAllCubes(cubeSelect[cubeSelect.length-1][cubeSelectNow[n][0]]);
				 cubeSelect[cubeSelect.length-1].splice(cubeSelectNow[n][0],1);
			  }
			cubeSelectNow = [];
			worker.clearNochildParent(cubeSelect[cubeSelect.length-1]);
		 }
	}
	
worker.removeAllCubes = function(mass){
		if (mass[0])
		   {				 
		   cubeSelect.push(mass);
		   var massLength = mass.length;
		 	for (var n=massLength-1;n>=0;n--)
			    {
				 worker.removeAllCubes(mass[n]);
				 cubeSelect[cubeSelect.length-1].splice(n,1);
				}			
				cubeSelect.pop();
			}
		else 
		{
			mass.transform.parent = null;
			cubeCoordArray[mass.x][mass.y][mass.z] = null;
		}
	}
	
worker.reComputeBounding = function(mass){
	//更新mass下的所有元素的bounding，这个是只更新到这个MASS不再网上更新。速度比较慢
	mass.boundingBox = null;
	var bounding;
	if(mass[0]){
	for(var n=0;n < mass.length;n++){
		bounding = worker.reComputeBounding(mass[n]);
		mass.boundingBox =  mass.boundingBox ? 
							mass.boundingBox.add(bounding):
							bounding;
			}
		}
		else{
			mass.comBounding();
			}
	return 	mass.boundingBox;
	}
	

worker.reAddBoundingByChild = function(mass){
	//从新检索这个MASs下的子集的bounding来从新构建这个bounding，并向上更新。是构建，这里并没有对子集的bounding进行重新计算。
	if(mass != cubeMass){
		mass.boundingBox = mass[0].boundingBox
		for(var n=1;n < mass.length;n++){
			mass.boundingBox = mass.boundingBox.add(mass[n].boundingBox);
			}
		worker.reAddBoundingByChild(mass.parent);
		}
	}
	
worker.selectAllExist = function(){
	var allExistArray = new Array;
	creator.removeAllFrames();
	for (var n = 0;n < cubeSelect[cubeSelect.length-1].length;n++){
		allExistArray[n] = [];
		allExistArray[n][1] = [];
		allExistArray[n][3] = [];
		allExistArray[n][0]= n ;
		allExistArray[n][2] = cubeSelect[cubeSelect.length-1][n];
		allExistArray[n][4] = cubeSelect[cubeSelect.length-1][n];
		}
		cubeSelectNow = allExistArray;
	}


worker.packCube = function(){
	if(cubeSelectNow[0]){
		var mass = cubeSelect[cubeSelect.length-1];
		worker._packCubeAt(mass);
		creator.removeAllFrames();
		var selectArray = new Array;
		selectArray[0] = mass.length-1;
		selectArray[1] = null;
		selectArray[2] = mass[mass.length-1];
		selectArray[3] = cubeSelectNow[cubeSelectNow.length-1][3];
		selectArray[4] = null;
		cubeSelectNow = [];
		cubeSelectNow.push(selectArray);
		}
	}

worker._packCubeAt = function(mass){
		var massPack = new Array;
		
		massPack.transform = cubePack.createObject("Transform");
		massPack.transform.parent = mass.transform;
		massPack.x = 0;
		massPack.y = 0;
		massPack.z = 0;
		massPack.localX = 0;
		massPack.localY = 0;
		massPack.localZ = 0;
		massPack.parent = mass;
		var num;
		var selectNum = cubeSelectNow.length;
		for(var n=0;n < selectNum;n++){
			massPack.push(cubeSelectNow[n][2]);
			cubeSelectNow[n][2].parent = massPack;
			cubeSelectNow[n][2].transform.parent = massPack.transform;
			massPack.boundingBox = massPack.boundingBox ? 
								massPack.boundingBox.add(cubeSelectNow[n][2].boundingBox):
								cubeSelectNow[n][2].boundingBox;
			}
		for(var n=selectNum-1;n>=0;n--){
			var selectInfo = cubeSelectNow[n];
			num = selectInfo[0];
			mass.splice(num,1);
			}
		mass.push(massPack);
	}
	
worker.unPackCube = function(){
	var mass = cubeSelect[cubeSelect.length-1];
	worker._unPackCubeAt(mass);
	}

worker._unPackCubeAt = function(mass){
	if (cubeSelectNow.length == 1 && cubeSelectNow[0][2][0]){
		var num = cubeSelectNow[0][0];
		var thisMass = cubeSelectNow[0][2];
		creator.clearWireFrame(0);
		cubeSelectNow = [];
			var selectNum = thisMass.length;
			for(var n=0;n < selectNum;n++){
				thisMass[n].parent = mass;
				thisMass[n].transform.translate(thisMass.localX,
												thisMass.localY,
												thisMass.localZ);
				thisMass[n].transform.parent = mass.transform;
				thisMass[n].localX += thisMass.localX;
				thisMass[n].localY += thisMass.localY;
				thisMass[n].localZ += thisMass.localZ;
				mass.push(mass[num][n]);
				}
		mass.splice(num,1);
		}
	}



worker.changeCube = function(){
	for(n=0;n < cubeSelectNow.length;n++){
		if(cubeSelectNow[n][2][0]){
			worker._changeSubCube(cubeSelectNow[n][2],
									defultValue.red,
									defultValue.green,
									defultValue.blue,
									defultValue.alpha)
		}
		else{
			cubeSelectNow[n][2].red = defultValue.red;
			cubeSelectNow[n][2].green = defultValue.green;
			cubeSelectNow[n][2].blue = defultValue.blue;
			cubeSelectNow[n][2].alpha = defultValue.alpha;
			cubeSelectNow[n][2].changeColor(defultValue.red,
											defultValue.green,
											defultValue.blue,
											defultValue.alpha);
			}
		}
	}

worker._changeSubCube = function(mass,r,g,b,alpha){
	if(mass[0]){
		var massLength = mass.length;
		for(var n=0;n < massLength;n++){
			worker._changeSubCube(mass[n],r,g,b,alpha);
			}
		}
		else{
			mass.changeColor(r,g,b,alpha);
			}
	}




//更新用来拉升cube的碰撞平面的bundingbox
worker.buttPlaneBunding = function(mass,direction){
	var transform = cubePack.createObject("Transform");
	transform.parent = cubeClient.root;
	transform.addShape(shape[2].planShape);
	var returnDirection = direction;
	var mass = mass;
	if (returnDirection[4]||returnDirection[5]){
						if (Math.abs(eyeAngle[1]) < PI/18){
							transform.translate([mass.x-mass.l/2,0,0]);
							transform.scale([1,2048,2048]);
							transform.rotateY(PI/2);
							}
						else {
							transform.translate([0,mass.y+mass.l/2,0]);
							transform.scale([2048,1,2048]);
							transform.rotateX(PI/2);
							}
							}	
		else if(returnDirection[3]||returnDirection[2]) {
							if (Math.abs(eyeAngle[0]) > PI/4 && Math.abs(eyeAngle[0]) < 3*PI/4){
								transform.scale([2048,2048,1]);
								transform.translate([0,0,mass.z-mass.l/2]);
								}
							else{
								transform.scale([1,2048,2048]);
								transform.translate([mass.x-mass.l/2,0,0]);
								transform.rotateY(PI/2);
								}
					}
			else {
					     if (Math.abs(eyeAngle[1]) < PI/18){
							transform.translate([0,0,mass.z-mass.l/2]);
							transform.scale([2048,2048,1]);
							}
						else {
							transform.translate([0,mass.y-mass.l/2,0]);
							transform.scale([2048,1,2048]);
							transform.rotateX(-PI/2);
							}
				}
	var boundingBox = transform.shapes[0].elements[0].getBoundingBox(0).mul(transform.worldMatrix);
	transform.parent = null;
	return boundingBox;
	}
	
worker.openCloseMoveMode = function(){
	if(!mouseButton["moveMode"]){
		mouseButton["moveMode"] = true;
		mouseButton["alphaTime"] = false;
		}
		else{
			mouseButton["moveMode"] = false;
			mouseButton["alphaTime"] = true;
			}
	}

worker.selectCubeByXYZ = function(x,y,z,directionArray){
	var unSelected = true;
	createCubeCoordArray(x,y);
	var testCube = cubeCoordArray[x][y][z];
	
	for(var n=0;n < cubeSelectNow.length;n++){
		if( cubeSelectNow[n][2]==testCube){
			creator.clearWireFrame(n);
			unSelected = false;
			for(var nDire=0;nDire < 6;nDire++){
				cubeSelectNow[n][3][nDire] = cubeSelectNow[n][3][nDire] || directionArray[nDire];
				}
			break;
			}
			
		}
	var num = worker.findCubeNum(testCube);
	if(unSelected && testCube && num!=undefined){
		var newSelected = [];
		newSelected[1] = [];
		newSelected[3] = directionArray;
		newSelected[0]= num;
		newSelected[2] = testCube;
		newSelected[4] = testCube;
		cubeSelectNow.push(newSelected);
		}
	}
worker.findCubeNum = function(mass){
	var thisParent = cubeSelect[cubeSelect.length-1];
	for(var n=0;n < thisParent.length;n++){
		if(thisParent[n]==mass){
			var num = n;
			break;
			}
		}
	return num;
	}
